<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getConData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getConData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          clearable
          placeholder="地址查询"
          auto-top
          @change="getConData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getConData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('签约放款（待签约）')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>
      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getConData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="项目信息" name="first">
          <info-view :related-data="relatedData"></info-view>
        </el-tab-pane>
        <el-tab-pane label="放款审批表">
          <loan-approve-view :query-id="queryId" view-mode></loan-approve-view>
        </el-tab-pane>
        <el-tab-pane label="合同审批表">
          <contract-approve
            :query-id="queryId"
            :process-status="relatedData.process_status"
            view-mode
          ></contract-approve>
        </el-tab-pane>
        <el-tab-pane label="合同信息" name="contract">
          <contract-guard-view :query-id="queryId"></contract-guard-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter">
          <letter-view :query-id="queryId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <attach-file :customerRelatedId="attachId" :visible.sync="attachVisible"></attach-file>

    <loan-approve
      :visible.sync="editVisible"
      @success="handleSuccess"
      :query-id="editId"
    ></loan-approve>
  </div>
</template>

<script>
import { enumGuatype, enumProcessStatus } from '@/credit/plugins/enumTown'
import { baseTableColumns } from '@/credit/views/county/unionProvince/Approve'
import { getEntrustContract } from '@/credit/views/county/unionProvince/components/contractGuardMixins'
import baseMixins from '@/bigcredit/views/self/fast/todoMixins'

import SelectAddress from '@/components/select/SelectAddress'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import AttachFile from '@/credit/views/city/program/Attach'
import AttachView from '@/credit/components/AttachView'
import ContractApprove from '@/bigcredit/views/self/fast/contract/components/Main'
import ContractGuardView from '@/credit/views/county/unionProvince/components/ContractGuardView'
import LoanApproveView from '@/bigcredit/views/self/fast/loan/components/Main'
import LoanApprove from '@/bigcredit/views/self/fast/loan/components/MainDialog'
import InfoView from '@/credit/views/city/bus/normal/Info'

export default {
  components: {
    SelectAddress,
    AttachView,
    AttachFile,
    LetterView,
    ContractApprove,
    ContractGuardView,
    LoanApproveView,
    LoanApprove,
    InfoView,
  },
  mixins: [baseMixins],
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'task_type.eq': 36,
        subKey: 'loan_approval.customer_related_id',
      },
      order: {
        N2create_time: 'DESC',
      },
      tableColumns: [
        ...baseTableColumns,
        { attrs: { label: '放款金额(万元)', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '合同签约时间', prop: 'signRegisterTime', align: 'center', width: 140 } },
        { attrs: { label: '放款银行', prop: 'replyBank', align: 'center' } },
        { attrs: { label: '审批模式', prop: 'processLabel', width: 80, align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', minWidth: 150 },
          operations: [
            {
              content: '放款审批申请',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row => row.editEnable,
            },
            // {
            //   content: '附件',
            //   icon: 'fa fa-clipboard',
            //   event: this.handleAttach
            // },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],

      activeName: 'first',

      // 档案卡
      queryId: '',
      reportVisible: false,
      relatedData: {},

      // 附件
      attachId: '',
      attachVisible: false,

      authPerson: {},
      editVisible: false,
      editId: '',
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getConData()
  },
  mounted() {},
  methods: {
    getConData(index = 1) {
      this.getData(index).then(() => {
        this.getEntrustContract()
      })
    },
    async getEntrustContract() {
      if (!this.tableData.length) return
      getEntrustContract(this.tableData).then(list => {
        this.tableData = this.tableData.map(it => {
          const entrust = list[it.customer_related_id] || {}
          it.replyBank = entrust.bank_name || '-'
          it.reply_funds = entrust.reply_funds || '-'
          return it
        })
      })
    },

    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.signRegisterTime = this.$utils.dateFormat(item.sign_register_time, this.dateFmt)
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name}`
      item.processLabel = enumProcessStatus[+item.process_status] || ''
      item.processStaff = '提示人'
      item.editEnable = true // item.res_a === this.userId
      return item
    },
    handleAttach(row) {
      this.attachId = row.customer_related_id
      this.attachVisible = true
    },
    handleEdit(row = {}) {
      this.editId = row.customer_related_id || ''
      this.editVisible = true
    },
    hanleView(row) {
      this.relatedData = row
      this.queryId = this.relatedData.customer_related_id
      this.reportVisible = true
    },
    beforeClose() {
      this.reportVisible = false
      setTimeout(() => (this.activeName = 'first'), 200)
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCountBusSelfFast')
      this.getConData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
